package net.lab1024.sa.admin.module.aneswork.pacu;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.aneswork.pacu.domain.PacuWorkEntity;
import net.lab1024.sa.admin.module.aneswork.pacu.domain.PacuWorkTimeEntity;
import net.lab1024.sa.admin.module.aneswork.pacu.domain.form.PacuWorkQueryForm;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.base.common.util.SmartLocalDateUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class PacuworkService {

    @Resource
    private PacuworkTimeDao pacuworkDao;

    @Resource
    private PacuWorkDao pacuWorkDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<PacuWorkEntity> queryPage(PacuWorkQueryForm queryForm) {
        queryForm.setCreatePeople(SmartRequestUtil.getRequestUser().getUserNum());
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<PacuWorkEntity> list = pacuWorkDao.queryPage(page, queryForm);
        PageResult<PacuWorkEntity> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加pacu记录
     */
    public ResponseDTO<String> add(PacuWorkEntity pacuWork) {
        pacuWork.setCreatePeople(SmartRequestUtil.getRequestUser().getUserNum());
        pacuWork.setCreateDateTime(SmartLocalDateUtil.toLocalDateTime(new Date()));
        pacuWorkDao.insert(pacuWork);
        return ResponseDTO.ok();
    }

    /**
     * 更新pacu记录
     *
     * @param pacuWork
     * @return
     */
    public ResponseDTO<String> update(PacuWorkEntity pacuWork) {
        pacuWorkDao.updateById(pacuWork);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除pacu记录
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }
        pacuWorkDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除pacu记录
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id){
            return ResponseDTO.ok();
        }
        pacuWorkDao.deleteById(id);
        return ResponseDTO.ok();
    }


    //查询个人pacu工作时间
    public ResponseDTO <PacuWorkTimeEntity> queryWorkTimesByUser() {
        // 创建QueryWrapper实例
        QueryWrapper<PacuWorkTimeEntity> queryWrapper = new QueryWrapper<>();

       // queryForm.setCreatePeople(SmartRequestUtil.getRequestUser().getUserNum());
        // 添加查询条件
        if (SmartRequestUtil.getRequestUser().getUserNum() != null && !SmartRequestUtil.getRequestUser().getUserNum().trim().isEmpty()) {
            queryWrapper.eq("create_people",SmartRequestUtil.getRequestUser().getUserNum());
        }

        // 执行查询
        PacuWorkTimeEntity pacuWorkTimeList = pacuworkDao.selectOne(queryWrapper);

        // 返回查询结果
        return ResponseDTO.ok(pacuWorkTimeList);
    }


    /**
     * 新增PACU时间工作量
     */
    public ResponseDTO<String> addTime(PacuWorkTimeEntity data) {
        data.setCreatePeople(SmartRequestUtil.getRequestUser().getUserNum());
        data.setCreateTime(SmartLocalDateUtil.toLocalDateTime(new Date()));
        pacuworkDao.insert(data);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> updateTime(PacuWorkTimeEntity data) {
        pacuworkDao.updateById(data);
        return ResponseDTO.ok();
    }


}
